#import "ProductBannerView.h"
#import "SDWebImage/SDWebImageManager.h"

#define kBannerHeightRatio 0.45  // 横幅高度占比
#define kPriceFontSize 24.0f     // 价格字体大小
#define kDescFontSize 14.0f      // 描述字体大小
#define kPadding 15.0f           // 内边距
#define kSpacing 10.0f           // 元素间距

@interface ProductBannerView ()

@property (nonatomic, strong) UIView *contentView;

@end

@implementation ProductBannerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    // 创建内容视图
    _contentView = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:_contentView];
    
    // 创建横幅图片视图
    _bannerImageView = [[UIImageView alloc] init];
    _bannerImageView.contentMode = UIViewContentModeScaleAspectFill;
    _bannerImageView.clipsToBounds = YES;
    [_contentView addSubview:_bannerImageView];
    
    // 创建价格标签
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = [UIFont boldSystemFontOfSize:kPriceFontSize];
    _priceLabel.textColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
    [_contentView addSubview:_priceLabel];
    
    // 创建价格标签
    _buyLabel = [[UILabel alloc] init];
    _buyLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    _buyLabel.textColor = kColorHEX(0x999999);
    [_contentView addSubview:_buyLabel];
    
    
    // 创建描述标签
    _descriptionLabel = [[UILabel alloc] init];
    _descriptionLabel.font = [UIFont systemFontOfSize:kDescFontSize];
    _descriptionLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    _descriptionLabel.numberOfLines = 0;
    [_contentView addSubview:_descriptionLabel];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = self.bounds.size.width;
    CGFloat currentY = 0;
    
    // 布局横幅图片
    CGFloat bannerHeight = width * kBannerHeightRatio;
    _bannerImageView.frame = CGRectMake(0, currentY, width, bannerHeight);
    currentY += bannerHeight + kPadding;
    
    // 布局价格标签
    [_priceLabel sizeToFit];
    _priceLabel.frame = CGRectMake(kPadding, currentY, _priceLabel.frame.size.width, _priceLabel.frame.size.height);
    currentY += _priceLabel.frame.size.height + kSpacing;
  
    _buyLabel.text = @"买家已经承诺技能真实，内容真实 >";
    [_buyLabel sizeToFit];
    _buyLabel.frame = CGRectMake(CGRectGetMaxX(_priceLabel.frame)+ 50, currentY-30, _buyLabel.frame.size.width, _buyLabel.frame.size.height);
//    currentY += _priceLabel.frame.size.height + kSpacing;
    
    // 布局描述标签
    CGFloat descWidth = width - 2 * kPadding;
    CGSize maxDescSize = CGSizeMake(descWidth, CGFLOAT_MAX);
    NSDictionary *descAttributes = @{NSFontAttributeName: _descriptionLabel.font};
    CGRect descRect = [_descriptionLabel.text boundingRectWithSize:maxDescSize
                                                              options:NSStringDrawingUsesLineFragmentOrigin
                                                           attributes:descAttributes
                                                              context:nil];
    _descriptionLabel.frame = CGRectMake(kPadding, currentY, descWidth, descRect.size.height);
}

- (void)setupWithImage:(UIImage *)image
                price:(NSString *)price
          description:(NSString *)description {
    _bannerImageView.image = image;
    _priceLabel.text = price;
    _descriptionLabel.text = description;
    
    [self setNeedsLayout];
}

- (void)setupWithImageUrl:(NSString *)imageUrl
                   price:(NSString *)price
             description:(NSString *)description
        placeholderImage:(UIImage *)placeholderImage {
    _priceLabel.text = price;
    _descriptionLabel.text = description;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    // 使用SDWebImage加载图片
//    [_bannerImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
//                        placeholderImage:placeholderImage];
     [manager loadImageWithURL:[NSURL URLWithString:imageUrl] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
         self->_bannerImageView.image = image;
    }];
    
    [self setNeedsLayout];
}

+ (CGFloat)calculateHeightForWidth:(CGFloat)width
                       description:(NSString *)description {
    if (!description || description.length == 0) {
        return 0;
    }
    
    // 计算描述文本高度
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:kDescFontSize];
    tempLabel.numberOfLines = 0;
    tempLabel.text = description;
    
    CGFloat descWidth = width - 2 * kPadding;
    CGSize maxDescSize = CGSizeMake(descWidth, CGFLOAT_MAX);
    [tempLabel sizeToFit];
    CGFloat descHeight = [tempLabel sizeThatFits:maxDescSize].height;
    
    // 计算总高度：横幅高度 + 价格高度 + 描述高度 + 间距
    CGFloat totalHeight = width * kBannerHeightRatio + kPriceFontSize + descHeight + 3 * kPadding + kSpacing;
    
    return totalHeight;
}

@end    
